<template>
  <el-aside width="250px" class="image-aside" v-loading="loading">
    <div class="top">
      <AsideList
        @click="handleChangeActiveId(item.id)"
        @edit="handleEdit(item)"
        @delete="handleDelete(item.id)"
        :active="activeId == item.id"
        v-for="item in list"
        :key="item.id"
        >{{ item.name }}</AsideList
      >
    </div>
    <div class="bottom">
      <el-pagination
        background
        layout="prev, next"
        :total="total"
        :current-page="curpage"
        @current-change="getData"
        :page-size="limit"
      />
    </div>
  </el-aside>
  <FormDrawer :title="drawerTitle" ref="formDrawerRef" @submit="handleSubmit">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="form.sort" :min="0" :max="1000" />
      </el-form-item>
    </el-form>
  </FormDrawer>
</template>

<script setup>
import { computed, reactive, ref } from "vue";
import FormDrawer from "./FormDrawer.vue";
import {
  getimageclass,
  createimageclass,
  updateimageclass,
  deleteimageclass,
} from "~/api/image_class.js";
import AsideList from "./AsideList.vue";
import { toast } from "../composables/util";

//加载动画
const loading = ref(false);
const list = ref([]);

const limit = ref(5);
//分页
const curpage = ref(1);
const total = ref(0);

//获取数据
function getData(p = null) {
  if (typeof p == "number") {
    curpage.value = p;
  }
  loading.value = true;
  getimageclass(curpage.value)
    .then((res) => {
      
      list.value = res.data;
      total.value = res.total;
      let item = list.value[0];
      if (item) {
        handleChangeActiveId(item.id);
      }
    })
    .finally((res) => {
      loading.value = false;
    });
}
getData();
const editId = ref(0);
const drawerTitle = computed(() => (editId.value > 0 ? "修改" : "新增"));
const formDrawerRef = ref(null);

const form = reactive({
  name: "",
  sort: 1,
});

const rules = {
  name: [{ required: true, message: "分类名称不能为空", trigger: "blur" }],
};

const formRef = ref(null);

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) return;
    formDrawerRef.value.showLoading();

    const fun = editId.value
      ? updateimageclass(editId.value, form)
      : createimageclass(form);

    fun
      .then((res) => {
        toast(drawerTitle.value + "成功");
        getData(editId.value ? curpage.value : 1);
        formDrawerRef.value.close();
      })
      .finally(() => {
        formDrawerRef.value.hideLoading();
      });
  });
};

//新增
const handleCreate = () => {
  editId.value = 0;
  form.name = "";
  form.sort = 1;
  formDrawerRef.value.open();
};

//修改
const handleEdit = (row) => {
  editId.value = row.id;
  form.name = row.name;
  form.sort = row.sort;
  formDrawerRef.value.open();
};

//删除
const handleDelete = (id) => {
  loading.value = true;
  deleteimageclass(id)
    .then((res) => {
      toast("删除成功");
      getData();
    })
    .finally(() => {
      loading.value = false;
    });
};
//选择图库分类id
const activeId = ref(0);
const emit = defineEmits(["change"]);
function handleChangeActiveId(id) {
  activeId.value = id;
  emit("change", id);
}

defineExpose({
  handleCreate,
});
</script>

<style>
.image-aside {
  border-right: 1px solid #eee;
  position: relative;
}
.image-aside .top {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 50px;
  overflow-y: auto;
}
.image-aside .bottom {
  position: absolute;
  bottom: 0;
  height: 50px;
  left: 0;
  right: 0;
  @apply flex items-center justify-center;
}
</style>
